package com.jerehedu.myapplication34.openhelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.jerehedu.myapplication34.entrty.Book;

import java.sql.SQLException;

/**
 * Created by lenovo on 2016/11/4.
 */
public class DataBaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String DB_NAME="jredu.db";
    private static final int DB_VERSION=1;
    private static DataBaseHelper dataBaseHelper;
    public DataBaseHelper(Context context) {
        super(context,DB_NAME,null,DB_VERSION);
    }

    public synchronized static DataBaseHelper getInstance(Context context){
        if(dataBaseHelper==null){
            dataBaseHelper=new DataBaseHelper(context);
        }
        return dataBaseHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTableIfNotExists(connectionSource, Book.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource,Book.class,true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
